# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=lz4
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.1.r656
pkgrel=1
pkgdesc="Very fast lossless compression algorithm (mingw-w64)"
arch=('any')
url="http://cyan4973.github.io/lz4/"
license=('BSD' 'GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "git")
checkdepends=('diffutils')
options=('staticlibs' 'strip')
source=("${_realname}::git+https://github.com/Cyan4973/lz4.git"
        001-mingw-install-over-msys.patch)
sha256sums=('SKIP'
            'c85c4e93db6634b42a2420aa1fc6908ed3152004ca37fada6223afd29e09e876')

pkgver() {
  cd ${_realname}
  local _MAJOR=`sed -n '/define LZ4_VERSION_MAJOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < lib/lz4.h`
  local _MINOR=`sed -n '/define LZ4_VERSION_MINOR/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < lib/lz4.h`
  local _PATCH=`sed -n '/define LZ4_VERSION_RELEASE/s/.*[[:blank:]]\([0-9][0-9]*\).*/\1/p' < lib/lz4.h`
  local _REV="$(git rev-list --count HEAD)"
  printf "%s.%s.%s.r%s" "${_MAJOR}" "${_MINOR}" "${_PATCH}" "${_REV//[[:alpha:]]}"

}

prepare() {
  cd ${_realname}
  patch -p1 -i ${srcdir}/001-mingw-install-over-msys.patch
  cd -

  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  cp -rf "${srcdir}/${_realname}" "${srcdir}/build-${MINGW_CHOST}"
}

build() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make CC=${MINGW_PREFIX}/bin/gcc
}

check() {
  build-${MINGW_CHOST}/programs/lz4 /etc/profile profile.lz4
  build-${MINGW_CHOST}/programs/lz4 -d profile.lz4 profile
  diff -q /etc/profile profile
  rm profile

  cd "${srcdir}/build-${MINGW_CHOST}"
  make CC=${MINGW_PREFIX}/bin/gcc test
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make CC=${MINGW_PREFIX}/bin/gcc DESTDIR="${pkgdir}" PREFIX=${MINGW_PREFIX} install
}
